package com.example.sls.controller.admin.defaultProSet;

import com.example.sls.pojo.admin.defaultProSet.DefaultProSet;
import com.example.sls.service.admin.defaultProSet.DefaultProSetService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/defaultProSet")
public class DefaultProSetController {
    @Autowired
    DefaultProSetService defaultProSetService;

    @GetMapping("/getPageInfo/{pageNum}/{pageSize}")
    public PageInfo<DefaultProSet> getAllProsByPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
    return defaultProSetService.getAllProByPage(pageNum,pageSize);
    }

    @GetMapping("/getPageInfoByProType/{proType}/{pageNum}/{pageSize}")
    public PageInfo<DefaultProSet> getPageInfoByProType(@PathVariable("proType") Integer proType,  @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        return defaultProSetService.getPageInfoByProType(proType, pageNum, pageSize);
    }


    @PostMapping("/addPro")
    public Map<String, String> addPro(@RequestParam Map<String,String> params){

        Integer proType = Integer.valueOf(params.get("proType"));
        String content = params.get("content");
        String correctSQL = params.get("correctSQL");

        System.out.println("========");
        System.out.println("controller-proType: " + proType);
        System.out.println("controller-content: " + content);
        System.out.println("controller-correctSQL: " + correctSQL);
        System.out.println("========");

        Map<String,String> result = new HashMap<>();
            if(defaultProSetService.addPro(proType, content, correctSQL)){
                result.put("error_message","success");
            }
            else{
                result.put("error_message","添加出错");
            }


            return result;
    }

    @PostMapping("/updatePro")
    public Map<String, String> updatePro(@RequestParam Map<String,String> params){

        Integer id = Integer.valueOf(params.get("id"));
        String content = params.get("content");
        String correctSQL = params.get("correctSQL");

        System.out.println("========");
        System.out.println("controller-id: " + id);
        System.out.println("controller-content: " + content);
        System.out.println("controller-correctSQL: " + correctSQL);
        System.out.println("========");

        Map<String,String> res = new HashMap<>();
        if(defaultProSetService.updatePro(id, content, correctSQL)){
            res.put("error_message","success");
        }
        else{
            res.put("error_message","添加出错");
        }


        return res;
    }


    @PostMapping("/deletePro")
    public Map<String,String> deletePro(@RequestParam Map<String,String> params){
        Map<String,String> result = new HashMap<>();
        Integer id = Integer.valueOf(params.get("id"));
        boolean flag = defaultProSetService.deleteProById(id);
        if(flag){
            result.put("error_message", "success");
        }
        else{
            result.put("error_message", "删除出错");
        }
        return result;
    }

    @GetMapping("/getInfoById")
    public DefaultProSet getProInfoById(@RequestParam Map<String,String> params){
        Integer id = Integer.valueOf(params.get("id"));
        System.out.println("========");
        System.out.println(id);
        System.out.println("========");
        return defaultProSetService.getProInfoById(id);
    }




}
